package com.haozi.ehub.admin.filter;

import com.haozi.ehub.core.util.CodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域过滤@WebFilter(filterName="CorsFilter" , urlPatterns="*")
 * @author ：terry
 * @date ：Created in 2020/7/2 14:44
 * @description：TODO
 * @version: 1.0
 */
@Slf4j
@Deprecated
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("Execute CorsFilter class doFilter method");
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        StringBuilder requestMethod = new StringBuilder(CodeUtils.GET_U);
        requestMethod.append(",").append(CodeUtils.PUT_U);
        requestMethod.append(",").append(CodeUtils.POST_U);
        requestMethod.append(",").append(CodeUtils.DELETE_U);
        requestMethod.append(",").append(CodeUtils.OPTIONS_U);

        httpServletResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, CodeUtils.ALL);
        httpServletResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.toString());
//        httpServletResponse.addHeader("Access-Control-Allow-Headers",
//                "Content-Type,X-Requested-With,accept,Origin," +
//                        "Access-Control-Request-Method,Access-Control-Request-Headers,token");
    }

    @Override
    public void destroy() {

    }
}
